<?php
defined('BASEPATH') or die('forbidden');

class gmcommand_controller extends Controller {

    private $inc;
    private $wholeServerHost;
    
    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        $this->inc=$this->config['inc'];
        
        $config = get_config();
        $this->wholeServerHost = $config['host_url'];
    }

    function index() {
        //print_r($_SESSION['gkl']);
        $areahostArray = get_area_list_option();
        $selectedArea = $_SESSION['area_selected_id'];
    	if ($selectedArea == '')
		{
			echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
		}
		
        $this->view->assign("arealist", $this->form->select("area_selected_id", $areahostArray, $selectedArea, array('id' => 'area_selected_id')));
        $this->view->display("seal/gmcommand/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        $sql="select * from gm_command where 1=1 ";
        if (empty($area_host))  
        {
        	$sql.=" and area_host='".$area_host."'";
        }
        $sql.=" order by insert_time desc LIMIT {$offset},{$rp}";
        $this->db->query($sql);
        $row=$this->db->row('default');
        
        
        foreach ($row as $key=>$value) {
            $rows[] = array('id' => $value['id'], 'cell' => $value);
        }
        $sql = "SELECT COUNT(*) AS num FROM gm_command where 1=1 ";
        if (empty($area_host))  
        {
        	$sql.=" and area_host='".$area_host."'";
        }
        $one = $this->db->one($sql);
        $total = $one['num'];
        
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

    function add($id = 0) {
        if (is_submit()) {
            $rowdata = $_POST['row'];
            
            //print_r($rowdata);
            
            $area_host=$rowdata['area_host'];
            unset($rowdata['area_host']);
            $_SESSION['role_name']=$rowdata['role_name'];   
            $_SESSION['area_host']=$area_host;
            //print_r($rowdata);
            if(!empty($rowdata['gm_command']))
            {
            	//传给服务器不需要此字段
            	$description=$rowdata['description'];
            	unset($rowdata['description']);
            	//print_r($rowdata);

            	$str = array_to_stringparam($rowdata);
                //print_r($str);
                $lower = strtolower($rowdata['gm_command']);
                if ($lower == "@@reload words")
                {
                	$tmpstr = $this->wholeServerHost . $str . "action=31"; //如果为@@load word则多发一条给帐号服的httpserver
                	//print_r($tmpstr);
                	//exit(0);
                	$strResult = vpost($tmpstr);  //发给帐号服的是额外的，不管成功状态了
                }
                //echo "++++++++";
                $str = $area_host . $str . "action=31";
                //print_r($str);
                //exit(0);
                $strResult = vpost($str);          
                $result=0;
                //echo " strResult = $strResult str = $str ";
                //操作失败或者连接失败
                if(0 == $strResult)
                {
                	//echo "success";
                	$rowdata['description']=$description;	//存数据库时，要把此字段加上
                    $rowdata['insert_time']=get_datetime(time());
                    $arealist=get_area_list_host();
                    $rowdata['area_name']=$arealist[$area_host];
                    $rowdata['area_host']=$area_host;
                    unset($rowdata['role_id']);
                    $rowdata['managername'] = $this->auth->fields['username'];
                    //print_r($rowdata);
                    $this->db->insert('gm_command',$rowdata);
                    insert_log(translate('MENU_SEAL_GMCOMMAND'),translate('ADD'),NULL,array("name"=>$rowdata['role_name'].'/'.$rowdata['gm_command']));
                }else 
                {
                	$result = 1;
                }
                //echo "str = $str ";
                cp_tips($result == 0 ? "success" : "error");
            }
            else
            {
                $url = $area_host . 'role_name=' . $rowdata['role_name'] . '&action=37';                
                $strResult = vpost($url);
                $result=0;
                if ($strResult != 0 || $strResult === FALSE) {
                    $result=1;
                } else {                  
                    //insert_log(translate('MENU_SEAL_GMCOMMAND'),translate('MENU_SEAL_PULL_ONLINE'),NULL,array("name"=>$rowdata['role_name']));
                }
                if($strResult===FALSE)
                {
                    $result=  translate('C_CONNECT_AREA_FIALED');
                }
                else
                {
                    $result=$this->inc['operationResult'][$strResult];
                }
                echo "<script>alert('".$result."');</script>";
            }          
        }
        
        $this->view->assign('role_name', isset($_SESSION['role_name']) ? $_SESSION['role_name']:"");
        $this->view->assign("arealist", $this->form->select("row[area_host]", get_area_list_host(), isset($_SESSION['area_host']) ? $_SESSION['area_host']:"", array('id' => 'area_host')));
        $this->view->display("seal/gmcommand/add");
    }


}

?>
